package com.acmetelecom.core;

import org.joda.time.DateTime;

class DaytimePeakPeriod {

	public boolean offPeak(DateTime time) {
		return time.getHourOfDay() < 7 || time.getHourOfDay() >= 19;
	}

	public static long getOnPeakSeconds(DateTime start, DateTime end) {
		long onPeakSeconds = 0;

		// not sure if this should give out an exception
		if (start.getMillis() > end.getMillis()) {
			return 0;
		}

		while (true) {
			if (start.getHourOfDay() < 7) {
				start = start.withTime(7, 0, 0, 0);
			} else if (start.getHourOfDay() >= 19) {
				start = start.plusDays(1).withTime(7, 0, 0, 0);
			}

			if (start.getDayOfMonth() < end.getDayOfMonth()) {
				onPeakSeconds += 12 * 60 * 60;
				start = start.plusDays(1);
			} else {
				if (end.getHourOfDay() >= 7) {
					if (end.getHourOfDay() >= 19) {
						end = end.withTime(19, 0, 0, 0);
					}
					onPeakSeconds += (end.getMillis() - start.getMillis()) / 1000;
				}
				break;
			}
		}

		return onPeakSeconds;
	}

	public static long getOffPeakSeconds(DateTime start, DateTime end) {
		long onPeakSeconds = getOnPeakSeconds(start, end);
		return ((end.getMillis() - start.getMillis()) / 1000) - onPeakSeconds;
	}
}
